package edu.pku.se.chocolate.UI.View;

import java.util.ArrayList;
import java.util.List;

import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.UI.Adapter.BBSlistAdapter;
import edu.pku.se.chocolate.datastructure.Category;
import edu.pku.se.chocolate.datastructure.Tag;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddselfDefTagView extends AddpreDefTagView{
	
	private TextView bbsTV;
	private bbsSrcSetView bbsSV;
	private EditText kwet;
	private EditText tget;
	public String bbsSelected;
	public int indexBBS = 5;
	public String[] selfSrc = {
			new String("portal"), 
			new String("lecture"),
			new String("hall"), 
			new String("oir"), 
			new String("dean"), 
			new String("BBS")
	};
	
	List<String> selfList;
	
	public AddselfDefTagView(final View _v, Category _cg)
	{
		super(_v, _cg);
//		Log.v("s1 ", "--");
		LayoutInflater lif = LayoutInflater.from(mainActivity);
//		Log.v("s2 ", "--");
		View bbssetting = lif.inflate(edu.pku.se.chocolate.R.layout.bbssetting, null);
//		Log.v("s3 ", "--");
		bbsSV = new bbsSrcSetView(bbssetting, mainActivity);
		bbsSV.setBBSsetListener(bbssetting, this, mainActivity);
//		Log.v("s4 ", "--");
		selfList = new ArrayList<String>();
		for(int i = 0; i < selfSrc.length; i ++)
		{
			selfList.add(selfSrc[i]);
		}
		bbsTV = (TextView)_v.findViewById(edu.pku.se.chocolate.R.id.selfTagbbstv);

//		Log.v("s5 ", "--");
		Spinner sp = (Spinner)_v.findViewById(edu.pku.se.chocolate.R.id.selfTagSrcspinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(mainActivity, android.R.layout.simple_spinner_item, selfList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp.setAdapter(adapter);
		sp.setSelection(0);	
		
		TextView tv = (TextView)_v.findViewById(R.id.selfToptv);
		tv.setText("*添加标签*");
		tv.setTextAppearance(mainActivity, android.R.style.TextAppearance_Large);
		tv.setGravity(Gravity.CENTER);
		
		kwet = (EditText)_v.findViewById(R.id.selfTagkwet);
		tget = (EditText)_v.findViewById(R.id.selfTagNameet);
		bbsSelected = "";
		

		sp.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) 
			{
				// TODO Auto-generated method stub
				TextView tv = (TextView)_v.findViewById(edu.pku.se.chocolate.R.id.selfTagbbstv);
				
				if(selfSrc[arg2].equals("BBS"))
				{
					tv.setVisibility(View.VISIBLE);
					releaseFocus();
					bbsSV.show();
				}
				else
				{
					tv.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		
	}
	
	public void releaseFocus()
	{
		View focusV = mainActivity.getCurrentFocus();
		if(focusV != null)
		{
			focusV.setFocusable(false);
			focusV.setFocusableInTouchMode(false);
			focusV.clearFocus();
		}
	}
	
	public void setBBSsrc(String _bbs)
	{
		bbsSelected = _bbs;
//		Log.v("call setbbssrc", _bbs);
	}
	
	public void setSelfButtonListener(View _v)
	{
		final View v = _v;
//		Log.v("s1 ", "--");
		Button bt_no = (Button)v.findViewById(edu.pku.se.chocolate.R.id.selfCancel);
//		Log.v("s2 ", "--");
		Button bt_yes = (Button)v.findViewById(edu.pku.se.chocolate.R.id.selfConfirm);
//		Log.v("s3 ", "--");
//		Log.v("s3-1 ", "--");
		bt_no.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// back to message ui
				mainActivity.onBackPressed();
			}
			
		});
		
//		Log.v("s4 ", "--");
		final TextView _bbstv = bbsTV;
		
		bt_yes.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// get data		
				
				int tag_id = 0;
				int msg_id = -1;
				int msg_num = 0;
				int type = 2;
				
				EditText kwV = (EditText)v.findViewById(edu.pku.se.chocolate.R.id.selfTagkwet);
				EditText tgV = (EditText)v.findViewById(edu.pku.se.chocolate.R.id.selfTagNameet);
				
				String kwStr = null;
				String tgStr = null;
				
				kwStr = kwV.getText().toString();
				tgStr = tgV.getText().toString();				
			
				
				CheckBox cb = (CheckBox)v.findViewById(edu.pku.se.chocolate.R.id.selfTagremindcb);
				
				int isNotify = 0;
				if(cb.isChecked())
				{
					isNotify = 1;
				}
				
				Spinner sp = (Spinner)v.findViewById(edu.pku.se.chocolate.R.id.selfTagSrcspinner);
				String spStr = selfSrc[sp.getSelectedItemPosition()];
				String srcStr = spStr;
				
				if(spStr.equals("BBS"))
				{
					type = 3;
					if(AddselfDefTagView.isbbsSrc(bbsSelected) == false)
					{
						Toast.makeText(ChocolateView.mainActivity, "请选择正确的BBS版面", Toast.LENGTH_SHORT).show();
						_bbstv.setText("");
						sp.setSelection(0);
						bbsSelected = "";
						return;
					}
					srcStr = bbsSelected;					
				}
				
				
				if(tgStr.equals(""))
				{
					tgStr = kwStr;
				}
				
				if(tgStr.equals(""))
				{
					Toast.makeText(ChocolateView.mainActivity, "关键字和标签名不能同时为空", Toast.LENGTH_SHORT).show();
					return;
				}
				
				kwV.setText("");
				tgV.setText("");

				Tag tg = new Tag(tag_id, AddselfDefTagView.this.AtCg.getStorageName(), tgStr, srcStr, kwStr, type, msg_num, msg_id, isNotify);
				ChocolateActivity.c_solver.addTag(tg);
//				boolean addRet = ChocolateActivity.c_solver.addTag(tg);
//				if(addRet)
//				{
//					Log.v("addRet", "ok");
//				}
//				else
//				{
//					Log.v("addRet", "no");
//				}

//				Log.v("kw", "kkk" + kwStr);
//				Log.v("tg", "ttt" + tgStr);
//				Log.v("type", type + "");
//				Log.v("srcStr", srcStr);
//				Log.v("cb", isNotify + "");				
				
				mainActivity.onBackPressed();
			}
			
		});
		
		bbsTV.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				releaseFocus();
				bbsSV.show();
			}
			
		});
		
//		Log.v("s5 ", "--");
	}
	
	public static boolean isbbsSrc(String _src)
	{
//		Log.v("call isbbssrc", BBSlistAdapter.totalList.size() + "");
		BBSlistAdapter.totalList = ChocolateActivity.c_solver.getBBSAllForumName();
		for(int i = 0; i < BBSlistAdapter.totalList.size(); i ++)
		{
			if(_src.equals(BBSlistAdapter.totalList.get(i)))
			{
				return true;
			}
		}		
		return false;
	}
	
	public void showBBSsrc()
	{
//		Log.v("call showBBSsrc", bbsSelected);
		if(bbsTV.getVisibility() == View.VISIBLE)
		{
//			Log.v(" showBBSsrc visible", bbsSelected);
			if(isbbsSrc(bbsSelected))
			{
//				Log.v("showBBSsrc", "true");
				bbsTV.setText(bbsSelected + "(点击修改)");
			}
			else
			{
//				Log.v("showBBSsrc", "false");
				bbsTV.setVisibility(View.GONE);
				Spinner sp = (Spinner)self.findViewById(edu.pku.se.chocolate.R.id.selfTagSrcspinner);
				sp.setSelection(0);
			}
		}
	}
	
	public void selfInitial()
	{
		tget.setText("");
		kwet.setText("");
		bbsTV.setVisibility(View.GONE);
		bbsSelected = "";
		Spinner sp = (Spinner)self.findViewById(edu.pku.se.chocolate.R.id.selfTagSrcspinner);
		sp.setSelection(0);
	}

	public void show()
	{
		ChocolateActivity.c_solver.setautoReflash(null,null);
		showBBSsrc();
		bbsSV.moveFocus();
		tget.setFocusable(true);
		tget.setFocusableInTouchMode(true);
		kwet.setFocusable(true);
		kwet.setFocusableInTouchMode(true);
		super.show();
	}

}
